#!/usr/bin/env python
# encoding: utf-8

import paho.mqtt.client as mqtt
import re
import time
import threading
from datetime import datetime

MQTT_BROKER_URL = 'localhost'
MQTT_BROKER_PORT = 1883
MQTT_CLIENT_ID = "17740171896"
MQTT_USERNAME = ''
MQTT_PASSWORD = ''
MQTT_KEEPALIVE = 60
MQTT_TLS_ENABLED = False

NODE_ONLINE       = "A1"
NODE_OFFLINE      = "A2"
NODE_OPEN_TAP     = "A3"
NODE_CLOSE_TAP    = "A4"
NODE_OPEN_TAP_OK  = "A5"
NODE_CLOSE_TAP_OK = "A6"

SITE_ONLINE = "B1"
SITE_OFFLINE = "B2"

REG_TELEPHONE_SITE = re.compile(r'^user/([0-9]{11})/site/([a-z0-9]{8})')
REG_TELEPHONE_NODE = re.compile(r'^user/([0-9]{11})/node/([a-z0-9]{8})')
# REG_DEVICE_NODE = re.compile(r'^dev/([a-z0-9]{8})/node/([a-z0-9]{8})')
# REG_DEVICE_SITE = re.compile(r'^dev/([a-z0-9]{8})/site/([a-z0-9]{8})')

def log_style(msg):
    print('wx'+'*'*50)
    print('time: ',datetime.today())
    print('print: ',msg)

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    log_style("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    #  client.subscribe("$SYS/#")
    client.subscribe("user/%s/#"%MQTT_CLIENT_ID)

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    log_style(msg.topic+" "+str(msg.payload))
    topic = msg.topic
    payload = msg.payload.decode('utf-8')
    # 节点
    if REG_TELEPHONE_NODE.match(topic):
        res = REG_TELEPHONE_NODE.match(topic)
        cid = res.group(2)
        if payload == NODE_ONLINE:
            print('NODE_ONLINE', NODE_ONLINE)
        if payload == NODE_OFFLINE:
            print('NODE_OFFLINE', NODE_OFFLINE)
        if payload == NODE_OPEN_TAP_OK:
            print('NODE_OPEN_TAP_OK', NODE_OPEN_TAP_OK)
        if payload == NODE_CLOSE_TAP_OK:
            print('NODE_CLOSE_TAP_OK', NODE_CLOSE_TAP_OK)
    # 站点
    if REG_TELEPHONE_SITE.match(topic):
        res = REG_TELEPHONE_SITE.match(topic)
        cid = res.group(2)
        if payload == SITE_ONLINE:
            print('SITE_ONLINE', SITE_ONLINE)
        if payload == SITE_OFFLINE:
            print('SITE_OFFLINE', SITE_OFFLINE)


client = mqtt.Client(client_id=MQTT_CLIENT_ID)
client.on_connect = on_connect
client.on_message = on_message

client.connect(MQTT_BROKER_URL, MQTT_BROKER_PORT, MQTT_KEEPALIVE)

def do_something():
    while True:
        time.sleep(2)
        client.publish('dev/53010000/node/4e010001', payload=NODE_OPEN_TAP)
        client.publish('dev/53010000/node/4e010002', payload=NODE_CLOSE_TAP)
        client.publish('server/1/%s/get/devices'%MQTT_CLIENT_ID, payload=NODE_CLOSE_TAP)
th_do = threading.Thread(target=do_something)
th_do.setDaemon(True)
th_do.start()
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()


